import sys
import time

import torch.utils
import torch.utils.data

sys.path.append('src')

import tasks.bvd.bvd_run as graph_constructor
from data.api import iter_dataset
import pickle
import torch
from torch_geometric.data import Data,Batch
from torch_geometric.loader import DataLoader
import nn.api as nn_api
import nn.simplest_gcn as simplest_gcn
import os
import numpy as np

from collections import Counter

a_file='preprocessed_data/julia/CWE121_Stack_Based_Buffer_Overflow/s01/bad/CWE121_Stack_Based_Buffer_Overflow__char_type_overrun_memcpy_01/cfg_graph.json'
b_file='preprocessed_data/julia/CWE121_Stack_Based_Buffer_Overflow/s01/good/CWE121_Stack_Based_Buffer_Overflow__char_type_overrun_memcpy_01/cfg_graph.json'

a_cfg,b_cfg=graph_constructor.json_constructor(a_file, 'cfg'),graph_constructor.json_constructor(b_file, 'cfg')

a,b=a_cfg.construct_by_inst(),b_cfg.construct_by_inst()

#one_hot_dict = {"xor": 0, "mov": 1, "pop": 2, "and": 3, "push": 4, "lea": 5, "call": 6, "add": 7, "sub": 8, "jmp": 9, "cmp": 10, "je": 11, "nop": 12, "leave": 13, "ret": 14, "test": 15, "hlt": 16, "div": 17, "imul": 18, "shr": 19, "shl": 20, "sar": 21, "movabs": 22, "setb": 23, "movzx": 24, "setne": 25, "jae": 26, "jne": 27, "jle": 28, "jbe": 29, "pxor": 30, "movaps": 31, "movq": 32, "js": 33, "cdqe": 34, "not": 35, "sete": 36, "jg": 37, "enter": 38, "rol": 39, "fadd": 40, "jb": 41, "rep stosq": 42}

instrus_a=[e.features for e in a.inst_nodes]
instrus_b=[e.features for e in b.inst_nodes]

same=0

for i in range(len(instrus_a)):
    if instrus_a[i]==instrus_b[i]:
        same+=1

print(a_file)
print(b_file)
print(f"汇编代码相似度: {same/len(instrus_a)}")

# counter_a=Counter(instrus_a)
# counter_b=Counter(instrus_b)

# print(counter_a)
# print(counter_b)

